import 'package:app/pages/home.dart';
import 'package:app/pages/my.dart';
import 'package:app/pages/search.dart';
import 'package:app/pages/travel.dart';
import 'package:flutter/material.dart';

class Nav extends StatefulWidget {
  @override
  _NavState createState() => _NavState();
}

class _NavState extends State<Nav> {
  // 接收一个StatefulWidget的泛型
  final _controller = PageController(
    initialPage: 0,
  );

  final _defaultColor = Colors.black;
  final _activeColor = Colors.blue;
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        children: <Widget>[
          Home(),
          Search(),
          Travel(),
          My(),
        ],
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        //底部导航
        type: BottomNavigationBarType.fixed, //固定图片
        currentIndex: _currentIndex,
        onTap: (index) {
          print(index);
          _controller.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home, color: _defaultColor),
            activeIcon: Icon(Icons.home, color: _activeColor),
            title: Text(
              '首页',
              style: TextStyle(
                color: _currentIndex == 0 ? _activeColor : _defaultColor,
              ),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.search, color: _defaultColor),
            activeIcon: Icon(Icons.search, color: _activeColor),
            title: Text(
              '搜索',
              style: TextStyle(
                color: _currentIndex == 1 ? _activeColor : _defaultColor,
              ),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.camera_alt, color: _defaultColor),
            activeIcon: Icon(Icons.camera_alt, color: _activeColor),
            title: Text(
              '旅拍',
              style: TextStyle(
                color: _currentIndex == 2 ? _activeColor : _defaultColor,
              ),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.account_circle, color: _defaultColor),
            activeIcon: Icon(Icons.account_circle, color: _activeColor),
            title: Text(
              '我的',
              style: TextStyle(
                color: _currentIndex == 3 ? _activeColor : _defaultColor,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
